//
//  MapKitDemo.m
//  ZooNgonDoiOS
//
//  Created by Trung sound on 7/19/12.
//  Copyright (c) 2012 Personal. All rights reserved.
//

#import "MapKitDemo.h"

@implementation MapKitDemo
@synthesize mapView, locationManager,location, geoCoder;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    lbLocation = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 300, 20)];
    mapView = [[MKMapView alloc]initWithFrame:CGRectMake(0, 40, 320, 300)];
    
    [mapView setMapType:MKMapTypeStandard];
    
    mapView.showsUserLocation = YES;
    
    [mapView setDelegate:self];
    
    locationManager = [[CLLocationManager alloc]init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    
    [locationManager startUpdatingLocation];
//    [locationManager startUpdatingHeading];

    [self.view insertSubview:mapView atIndex:0];
    [self.view insertSubview:lbLocation atIndex:1];
//    [self.view addSubview:lbLocation];
    
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    
    location = newLocation.coordinate;
    
    
    geoCoder = [[CLGeocoder alloc]init];
    [geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error){
        if (error) {
            NSLog(@"%@", [error localizedDescription]);
            return;
        }
        CLPlacemark *placemark = [placemarks objectAtIndex:0];
        lbLocation.text = [NSString stringWithFormat:@"%@, %@", placemark.thoroughfare, placemark.administrativeArea];
        
        for (id object in placemarks) {
            CLPlacemark *placemark1 = object;
            NSLog(@"1%@ 2%@ 3%@ 4%@",placemark1.thoroughfare, placemark1.administrativeArea, placemark1.country, placemark1.name);
        }
    } ];

    CLLocation *a = [[CLLocation alloc]initWithLatitude:21.0418241 longitude:105.8401011];
    CLLocationDistance distance = [newLocation distanceFromLocation:a];
    NSLog(@"%f",distance);
    
    MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } }; 
    region.center = location;
//    region.center.latitude = 21.0418241 ;
//    region.center.longitude = 105.8401011;
    region.span.longitudeDelta = 0.01f;
    region.span.latitudeDelta = 0.01f;
    [mapView setRegion:region animated:YES];
    NSLog(@"%f, %f",location.latitude, location.longitude);
    [locationManager stopUpdatingHeading];
    [locationManager stopUpdatingLocation];
    
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    NSLog(@"Local service is disable");
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
